package com.sb.hot.controller;

import java.net.URL;
import java.net.URLClassLoader;

public class MyClassLoaderCustom extends URLClassLoader {

    private final ClassLoader parentClassLoader;

    public MyClassLoaderCustom() {
        super(new URL[]{MyClassLoaderCustom.class.getResource("/")});
        this.parentClassLoader = Thread.currentThread().getContextClassLoader();
    }

    @Override
    protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
        if (!name.startsWith("com.sb.hot")) {
            try {
                return parentClassLoader.loadClass(name);
            } catch (Exception e) {
                //忽略
            }
        }

        // 缓存?
        // return defineClass(name, classBytes, 0, classBytes.length);
        // Class<?> aClass = super.loadClass(name, resolve);
        Class<?> aClass = super.findClass(name);
        if (null != aClass && resolve) {
            resolveClass(aClass);
        }
        return aClass;
    }
}
